#include "MonsterServer.h"
#include "Conversion.h"

#define MONSTER_IMAGE_HEIGHT 27.0
#define MONSTER_IMAGE_WIDTH 26.0

/*Surcharge operateur*/
sf::Packet& operator <<(sf::Packet& packet, const MonsterServer& monsterServer) {	
	sf::Vector2f position = box2dToSfmlPosition(monsterServer.monsterPosition);
	
	return packet << position.x << position.y << monsterServer.monsterDirection;
}

MonsterServer::MonsterServer(float posX, float posY) {
	monsterPosition = sfmlToBox2dPosition(sf::Vector2f(posX + (MONSTER_IMAGE_WIDTH/2), 
		posY + (MONSTER_IMAGE_HEIGHT/2)));

	monsterWidth = sfmlToBox2dLength(MONSTER_IMAGE_WIDTH/2);
	monsterHeight = sfmlToBox2dLength(MONSTER_IMAGE_HEIGHT/2);

	monsterDirection = RIGHT;

	m_contacting = false;
	type = MONSTER;
}

void MonsterServer::initialiseB2D(b2World* world) {
	monsterBodyDef.type = b2_kinematicBody;
	
	dynamicBox.SetAsBox(monsterWidth, monsterHeight);

	monsterBodyDef.position.Set(monsterPosition.x, monsterPosition.y);

	body = world->CreateBody(&monsterBodyDef);

	b2FixtureDef fixtureDef; 
	fixtureDef.shape = &(dynamicBox); 
	fixtureDef.density = 1.0f; 
	fixtureDef.friction = 0.3f;

	body->CreateFixture(&fixtureDef);
	body->SetUserData(this);
}

void MonsterServer::send(sf::TcpSocket& socket) {
	sf::Packet packet;
	packet << *this;
	socket.send(packet);
}

void MonsterServer::startContact() {
	m_contacting = true;
}

void MonsterServer::endContact() {
	m_contacting = false;
}